/**
 * Created by lwb on 2017/5/11.
 */
var hotsLoading = false;
var totalPage = 1;
var IPageNo = 1;
var curcount = 0;
/*全选*/
$("#allckb").click(function () {
    if (this.checked) {
        $(".selectbox input:checkbox").attr("checked", true);
    } else {
        $(".selectbox input:checkbox").attr("checked", false);
    }
});

//减少商品数量
$('.list').on('click', '.sy_minus', function () {
    var txt = $(this).next();
    var n = parseInt(txt.val());
    n--;
    txt.val(n);
    inputAmount(txt);
});

//输入数量
$('.list').on('change', '.num', function () {
    $(this).val($(this).val().replace(/[^0-9]/g, ''));
    if ($(this).val() == '0' || $(this).val() == '') $(this).val('1');
    if ($(this).attr("maxnum") && $(this).val()) {
        var max = parseInt($(this).attr("maxnum"));
        if (parseInt($(this).val()) > max) $(this).val(max);
    }
});

//数量焦点
$('.list').on('focus', '.num', function () {
    $(this).select();
});

//增加商品数量
$('.list').on('click', '.sy_plus', function () {
    var txt = $(this).prev();
    var n = parseInt(txt.val());
    n++;
    txt.val(n);
    inputAmount(txt);
});

//全选反选
$('.list').on('click', '.selectbox', function () {
    if ($(this).find("input").is(":checked")) {
        if ($(".list").find(":checked").length == curcount) {
            $("#allckb").attr("checked", true);
        }
    } else {
        $("#allckb").attr("checked", false);
    }
});

function loadProducts() {
    if (hotsLoading) {
        return false;
    } else {
        hotsLoading = true;
    }

    $w.httpRequest({
        url: parms.url,
        method: "get",
        data: parms.data,
        ok: function (ret) {
            hotsLoading = false;
            var data = ret.Data;
            var count = data.resultcount;
            if (count == 0) {
                $(".empty-page").css("display", "block");
                $(".shopping-cart").hide();
                $(".btn-size").hide();
            }
            totalPage = data.pagecount;
            productdata = data.list;
            curcount += productdata.length;
            if (productdata.length > 0) {
                for (var i = 0; i < productdata.length; i++) {
                    if (productdata[i].B_chanjia && productdata[i].B_chanjia.length > 11) {
                        productdata[i].B_chanjia = productdata[i].B_chanjia.substr(0, 11) + "...";
                    }
                }
                IPageNo++;
                $("#allckb").attr("checked", false);
                $(".shopping-cart").css("display", "block");
                $(".btn-size").css("display", "block");
                showProducts();
            } else {
                IPageNo = 0;
            }
        }
    })
}

function replaceHtmlFileserver(html){

    if(html){

        var sourceHtml = html;
        var fsIndex = -1;
        var targetHtml = "";
        do{
            fsIndex = sourceHtml.indexOf(fileserver);
            if( fsIndex >= 0 ){
                if( fsIndex != 0 ){
                    targetHtml += sourceHtml.substr( 0 , fsIndex );
                }
                targetHtml += getRandomFileserver();
                if( ( fsIndex + fileserver.length ) < ( sourceHtml.length - 1 ) ){
                    sourceHtml = sourceHtml.substring( fsIndex + fileserver.length );
                }
            } else {
                targetHtml += sourceHtml;
            }
        } while ( fsIndex >= 0 );

        return targetHtml;

    }
    return html;
}

function showProducts() {
    var h = template('productHtml_list', {
        ctx: ctx,
        fileserver: fileserver,
        isuserauth: isuserauth,
        islogin: islogin,
        list: productdata
    });
    var newHeml = replaceHtmlFileserver(h);
    $(".list").append(newHeml);
}

$(function () {
    loadProducts();
    var bottomHeight = $("div.h98").height() * 2;
    var scroll = new auiScroll({
        listen: true, //是否监听滚动高度，开启后将实时返回滚动高度
        distance: bottomHeight //判断到达底部的距离，isToBottom为true
    }, function (ret) {
        if (ret.isToBottom) {
            if (IPageNo > 0 && IPageNo <= totalPage) {
                var toast = new auiToast({});
                toast.loading({
                    title: "加载中",
                    duration: 300
                }, function () {
                    setTimeout(function () {
                        toast.hide();
                    }, 300)
                });

                loadProducts();
            }
        }
    });
});

function addToCart() {
    if(hotsLoading){
        return false;
    }else {
        hotsLoading = true;
    }

    var productArr = $(".list").find(":checked").map(function () {
        return $(this).val() + "_" + $(this).parents("li").find(".num").val();
    }).get();
    if (productArr.length > 0) {
        $w.httpRequest({
            url: apiroot + "/we/home/addToCart",
            data: {"idsAmounts": productArr},
            ok: function () {
                new auiToast().success({
                    title: "加入购物车成功",
                    duration: 800
                })
            },
            fail: function () {
                new auiToast().fail({
                    title: "加入购物车失败",
                    duration: 800
                });
            },
            all: function () {
                hotsLoading = false;
            }
        })
    } else {
        hotsLoading = false;
        new auiToast().fail({
            title: "请选择商品",
            duration: 800
        });
    }
}